﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TeacherHelp.DAL;

namespace TeacherHelp.BLL
{
    public class ExercisesManager
    {
        private readonly ExercisesService _exercisesService;

        public ExercisesManager()
        {
            _exercisesService = new DAL.ExercisesService();
        }

        public List<ViewModel.Exercises> GetExercisesAndSubject(string subjectName = null, string difficulty = null)
        {
            var sql = _exercisesService.GetExercisesInfoSQL();
            if (subjectName != "全部" && !string.IsNullOrWhiteSpace(subjectName))
            {
                sql += _exercisesService.GetSujbectWhereSQL(subjectName.ToInt());
            }
            if (difficulty != "全部" && !string.IsNullOrWhiteSpace(difficulty))
            {
                sql += _exercisesService.GetDifficultyWhereSQL(difficulty.ToInt());
            }
            sql += _exercisesService.GetOrderByDesc();
            try
            {
                Log.Info($"进行了获取题目信息和科目操作..subjectName={subjectName},difficulty={difficulty}");
                var result = DBHelper.GetListModel<ViewModel.Exercises>(sql);
                return result;
            }
            catch (Exception ex)
            {
                Log.Error($"获取题目信息和科目出现异常 subjectName={subjectName},difficulty={difficulty}", ex);
                return default;
            }
        }

        public ViewModel.Exercises GetExerciseModel(string Id)
        {
            var sql = _exercisesService.GetExercisesInfoSQL() + _exercisesService.GetIdWhereSQL(Id);
            try
            {
                Log.Info("根据id获取题目对象 Id=" + Id);
                var result = DBHelper.GetOneModel<ViewModel.Exercises>(sql);
                return result;
            }
            catch (Exception ex)
            {
                Log.Error($"根据id获取题目对象时出现异常! Id={Id}", ex);
                return default;
            }
        }

        public string ExercisesAdd(Model.Exercises exercises)
        {
            string erroInfo = null;
            try
            {
                Log.Info("添加题目中..");
                var sql = SQLBuilder.GetInsertSQLByModel(exercises);
                var result = DBHelper.ExecuteNonQuery(sql);
                if (result <= 0)
                {
                    erroInfo = "添加失败！";
                    Log.Info(erroInfo);
                }
                else
                {
                    Log.Info("题目添加成功！");
                }
            }
            catch (Exception ex)
            {
                Log.Error("添加题目时出现异常!", ex);
                erroInfo = "添加失败！";
            }
            return erroInfo;
        }

        public string ExercisesEdit(Model.Exercises exercises)
        {
            string erroInfo = null;

            try
            {
                Log.Info("编辑题目中..");
                var sql = SQLBuilder.GetUpdateSQLByModel(exercises) + _exercisesService.GetIdWhereSQL(exercises.Id);
                var result = DBHelper.ExecuteNonQuery(sql);
                if (result <= 0)
                {
                    erroInfo = "编辑失败！";
                    Log.Info(erroInfo);
                }
                else
                {
                    Log.Info("题目编辑成功!");
                }
            }
            catch (Exception ex)
            {
                Log.Error("编辑题目是出现异常！",ex);
            }
            return erroInfo;
        }

        public string ExercisesRemove(int Id)
        {
            string erroInfo = null;
            try
            {
                string sql = _exercisesService.GetDelExercisesSQL(Id);
                var result = DBHelper.ExecuteNonQuery(sql);
                if (result <= 0)
                {
                    erroInfo = "删除失败！";
                    Log.Info(erroInfo);
                }
                else
                {
                    Log.Info("删除题目成功！");
                }
            }
            catch (Exception ex)
            {
                Log.Error("删除题目时出现异常！",ex);
            }
            return erroInfo;
        }

        public List<ViewModel.TestExercises> ExercisesTest(int subjectId, int top = 50)
        {
            try
            {
                Log.Info("获取考试题目中..");
                string sql = _exercisesService.GetExercisesId(subjectId);
                List<int> idList = DBHelper.GetListModel<int>(sql);
                sql = _exercisesService.GetExercises(string.Join(",", idList.Take(top)));
                List<ViewModel.TestExercises> result = DBHelper.GetListModel<ViewModel.Exercises>(sql)
                .Select(m => new ViewModel.TestExercises
                {
                    Id = m.Id,
                    Content = m.Content,
                    Difficulty = m.Difficulty,
                    optionList = GetOption(m),
                    answerList = m.Answer.Split(',').ToList()
                }).ToList();
                result.Shuffle();
                return result;
            }
            catch (Exception ex)
            {
                Log.Error("获取考试题目出现异常！",ex);
                return default;
            }
        }

        private List<ViewModel.Option> GetOption(ViewModel.Exercises m)
        {
            List<ViewModel.Option> optionList = new List<ViewModel.Option>();

            if (!string.IsNullOrWhiteSpace(m.Aoption))
                optionList.Add(new ViewModel.Option { value = m.Aoption, key = "A" });

            if (!string.IsNullOrWhiteSpace(m.Boption))
                optionList.Add(new ViewModel.Option { value = m.Boption, key = "B" });

            if (!string.IsNullOrWhiteSpace(m.Coption))
                optionList.Add(new ViewModel.Option { value = m.Coption, key = "C" });

            if (!string.IsNullOrWhiteSpace(m.Doption))
                optionList.Add(new ViewModel.Option { value = m.Doption, key = "D" });

            return optionList;

        }
    }
}
